package com.higgins.lawapp.client.maintenance.components;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.higgins.lawapp.client.maintenance.handlers.DataItemTableHandler;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemInstanceDTO;

public class DataItemTable extends FlexTable	{
	private List<DataItemDTO> dataItemDTOs;
	private DataItemTableHandler dataItemTableHandler;

	public DataItemTable()	{
		super();
		initialise();
	}
	
	public void initialise()	{
		this.setCellPadding(1);
		this.setCellSpacing(0);
		this.setWidth("800px");
		
		RowFormatter rf = this.getRowFormatter();
		rf.setStyleName(0, "FlexTable-HeaderRow");

		addCheckBox(0,0);
		this.setText(0,1,"Item One");
		this.setText(0,2,"Item Two");
		addDeleteButton(0,3);
		this.getCellFormatter().addStyleName(0, 0,"FlexTable-checkBoxCell");
		this.getCellFormatter().addStyleName(0, 1,"FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 2,"FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 3,"FlexTable-deleteButtonCell");
		
	}
	public void addDataItemTableHandler(DataItemTableHandler dataItemTableHandler)	{
		this.dataItemTableHandler = dataItemTableHandler;
		addClickHandler(dataItemTableHandler);
	}
	
	public void setDataItemInstanceSelected(DataItemInstanceDTO dataItemInstanceDTOSelected)	{
		if (dataItemInstanceDTOSelected==null)
			setDataItemDTOs(null);
	}

	public void setDataItemDTOs(List<DataItemDTO> dataItemDTOs, List<String> dataItemTitles)	{
		Iterator<String> iterator = dataItemTitles.iterator();
		int index = 1;
		while (iterator.hasNext())	{
			String fieldTitle = iterator.next();
			if (fieldTitle!=null)
				this.setText(0,index, fieldTitle);
			else
				this.setText(0,index, "Item " + index);
			index++;
		}
		setDataItemDTOs(dataItemDTOs);
	}
	public void setDataItemDTOs(List<DataItemDTO> dataItemDTOs)	{
		this.dataItemDTOs = dataItemDTOs;
		update();
	}
	public void update()	{
		CheckBox checkBox = (CheckBox)getWidget(0,0);
		checkBox.setValue(false);
		int rowCount = getRowCount();
		int row = 1;
		while (rowCount > row)	{
			this.removeRow(1);
			row++;
		}
		if (dataItemDTOs!=null)	{
			Iterator<DataItemDTO> iterator = dataItemDTOs.iterator();
			row=1;
			while (iterator.hasNext())	{
				DataItemDTO dataItemDTO = iterator.next();
				addCheckBox(row, 0);
				setText(row, 1, dataItemDTO.getItemOne());
				setText(row, 2, dataItemDTO.getItemTwo());
				addDeleteButton(row, 3);
				getCellFormatter().addStyleName(row, 0,"FlexTable-checkBoxCell");
				getCellFormatter().addStyleName(row, 1,"FlexTable-normCell");
				getCellFormatter().addStyleName(row, 2,"FlexTable-normCell");
				getCellFormatter().addStyleName(row, 3,"FlexTable-deleteButtonCell");
	//			RowFormatter rf = this.getRowFormatter();
	//			rf.setStyleName(row, "FlexTable-NormRow");
				row++;
			}	
		}
//		setHeight("800px");
	}
	private void addCheckBox(int row, int column)	{
		CheckBox checkBox = new CheckBox();
		setWidget(row, column, checkBox);
		checkBox.setValue(false);
	}
	private void addDeleteButton(int row, int column)	{
		Button button = new Button("Delete");
		setWidget(row, column, button);
	}
	
}
